package com.googlecode.esblabs.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Util {

	static private final Random bodIdGenerator = new Random();
	
	public static XMLGregorianCalendar getCurrentDateTime() {
		return convertDateTimeToXMLGregorianCalendar(null);
	}

	public static XMLGregorianCalendar convertDateTimeToXMLGregorianCalendar(Date date) {
		try {
			GregorianCalendar gc = new GregorianCalendar();
			if (date != null) {
				gc.setTime(date);
			}
			DatatypeFactory f  = DatatypeFactory.newInstance();
			return f.newXMLGregorianCalendar(gc);
		} catch (DatatypeConfigurationException e) {
			throw new RuntimeException(e);
		}
	}

	public static String getBodId() {
		return Long.toHexString(bodIdGenerator.nextLong());
	}
	
	public static String getStacktrace(Throwable ex) {
		StringWriter sw = new StringWriter();
		ex.printStackTrace(new PrintWriter(sw));
		return sw.getBuffer().toString();
	}
	
}